package cn.yunhe.servlet;

import cn.yunhe.entity.LeaveMessage;
import cn.yunhe.entity.User;
import cn.yunhe.service.LeaveMessageService;
import cn.yunhe.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("userLeaveMessageServlet")
public class UserLeaveMessageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String  guestName =  request.getParameter("guestName");
       String guestTitle =  request.getParameter("guestTitle");
       String  guestContent = request.getParameter("guestContent");
        LeaveMessageService leaveMessageService = new LeaveMessageService();
        LeaveMessage leaveMessage = new LeaveMessage();
        User user = (User) request.getSession().getAttribute("Curr_User");
        leaveMessage.setUser(user);
        leaveMessage.setLeaveTitle(guestTitle);
        leaveMessage.setLeaveContent(guestContent);
        UserService userService = new UserService();
        leaveMessage.setLeaveId(userService.selectIdbyName(user).getUserid());
        Boolean b = leaveMessageService.addLeaveMessage(leaveMessage);
        if (b){
            request.getRequestDispatcher("guestbook.jsp").forward(request,response);
           // response.sendRedirect("guestbook.jsp");
        }else {
            System.out.println("error");

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
